﻿using System;
using System.Linq;
using PicoContainer;

namespace XEvolve.Core.Pico
{
    internal class PicoBasedExtensionPoint
    {
        private readonly IMutablePicoContainer container;

        public PicoBasedExtensionPoint(IMutablePicoContainer container)
        {
            this.container = container;
        }

        public T[] GetExtensionIdentifiers<T>()
        {
            return container.ComponentAdapters.Cast<IComponentAdapter>().Select(adapter => (T) adapter.ComponentKey).ToArray();
        }

        public T[] GetExtensions<T>()
        {
            return container.ComponentInstances.Cast<T>().ToArray();
        }

        public T GetExtension<T>(object extensionIdentifier)
        {
            return (T) container.GetComponentInstance(extensionIdentifier);
        }

        internal void RegisterExtension(object identifier, object instance)
        {
            container.RegisterComponentInstance(identifier, instance);
        }

        internal void RegisterExtension(object identifier, Type implementation)
        {
            container.RegisterComponentImplementation(identifier, implementation);
        }
    }
}